Skip to content

Exercise 2 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Exercise 2 #5

wants to merge 3 commits into from

Conversation

DusanRile
Copy link

No description provided.

TODO("Implement me!!")


// for ( i in 0 ..this.size - 1){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

@@ -20,7 +20,10 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference
*/

internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> {
TODO("Implement me!!")
// var par = Pair<Int, Int>(0, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.


return resultPair!!
// var resultPair: Pair<Int, Int>? = null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

}

internal fun List<Country>.findLanguageSpokenInMostCountries(): String {
TODO("Implement me!!!")
val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key
// println("Ovo je nesto: " + jezik)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

}

internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> {
TODO("Implement me!!!")

// var lista = listOf<Country>(this.first())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

}

internal fun List<Country>.findLanguageSpokenInMostCountries(): String {
TODO("Implement me!!!")
val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare variable names in English.

var first = this[0]
var second = this[0]
for (i in 0 ..< this.size -1){
if (this[i+1] >= first ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are accessing this[i + 1] many times, hence we assign this to variable nextNum and use nextNum variable everywhere else. This will improve readability.

second = this[i+1]
}
}
return Pair<Int, Int>(first, second)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <Int, Int> can be omitted since the compiler is smart enough to infer the generic type.

Also, there is an alternative way to declare a pair, and that is by using to infix extension function:
first to second

// Prolazak kroz jednu petlju
var first = this[0]
var second = this[0]
for (i in 0 ..< this.size -1){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we run your implementation for the input listOf(3, 1, 2), we would get the incorrect result Pair(3, 3).
Are the tests green for you?

nit: this.size -1 can be written as this.lastIndex

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test data for this method seems incomplete, and your implementation has a false positive in the tests, meaning the tests are green, but the actual implementation is incorrect.

In the above example for the input listOf(3, 1, 2) the expected output should be Pair(3, 2), and you implementation returns Pair(3, 3) which is incorrect. This is because the test didn't cover the case where the highest number in the list is at the first index.

I'm very sorry for the inconvenience with incomplete test data.

Please add a missing case to the test class and fix the bug in your implementation.

// }
// return resultPair!!

return this.sorted().let { it.first() to it[lastIndex] }
Copy link
Owner

@vuksa vuksa Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a mistake in the implementation where the lastIndex is stated. In the code you wrote, the lastIndex is the last index of the receiver object list. It's the same as if we wrote this.lastIndex.

TLDR: you should use it.lastIndex instead of this.lastIndex

In this case, the result will be correct, but if we, for example, filter elements of the list after sorting it, the lastIndex of the list we are operating on would change, and you would get an IndexOutOfBounds error.

Let's show this visually. In the following example, we have the list [3, 2, 4, 5, 1].
After calling sorted() on the list, we would get a new list [1, 2, 3, 4, 5], with the last index value being 4.
If after that, on the result list we call filter { it > 2 }, the new result list would be [3, 4, 5], where now the value of the last index is 2, and if we would call it[this.lastIndex] as in your code, we would get IndexOutOfBoundsException because this.lastIndex value is 4, which is greater than current last index.

@vuksa vuksa force-pushed the main branch 2 times, most recently from 9db668d to 189c667 Compare March 27, 2024 16:39
@vuksa vuksa added the reviewed label Apr 20, 2024
@vuksa vuksa force-pushed the main branch 3 times, most recently from 6106c56 to 8296ece Compare May 20, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants